<template>
  <div class="play-container"
       v-show="visible">
    <div class="full-mask-body">
      <div class="mask-header">
        <div class="close-icon-body"
             @click="close">
          <i class="el-icon-close"></i>
        </div>
      </div>
      <div class="mask-middle">
        <div class="middle-left">
          <div class="song-info">
            <img :src="albumImage" />
            <div class="info">歌曲名：{{player.name}}</div>
            <div class="info">歌手名：{{player.singer}}</div>
            <div class="info">专辑名：{{player.album_name}}</div>
          </div>
        </div>
        <div class="middle-content">
          <ul class="irc-list-mask"
              ref="container">
            <div class="irc-list-body"
                 :style="`transform: translate3d(0px, ${scrollHeight}px, 0px);`"
                 ref="lyricList">
              <li ref="lyricLine"
                  :class="{ actived: isActived(index) }"
                  v-for="(content,index) in irc"
                  :key="index">{{content.txt}}</li>
            </div>
          </ul>
        </div>
        <div class="middle-right">
          <div class="music-list">
            <div class="song-list"
                 ref="songContainer">
              <ul class="song-list-content">
                <li v-for="(item, index) in list"
                    :key="index"
                    class="song-item"
                    ref="songLine"
                    :class="{actived: isPlayIndex(item)}">
                  <div class="check">
                    <template v-if="isPlayIndex(item) && isPlaying">
                      <span class="playing-box">
                        <div class="playing-icon"></div>
                      </span>
                    </template>
                    <template v-else>
                      <span class="number">{{indexNum(index)}}</span>
                      <span class="play-icon">
                        <i class="iconfont no-hover">&#xe733;</i>
                        <i class="iconfont hover"
                           @click="playSong(item)">&#xe671;</i>
                      </span>
                    </template>
                  </div>
                  <div class="song-name">{{item.name}}</div>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      <div class="mask-footer">
        <action-bar :loop="loop"
                    @changeLoopEmit="changeLoop"
                    scences="normal"
                    :is-playing="isPlaying"
                    :progress-width="progressWidth"
                    :prev="prev"
                    :pause="pause"
                    :play="play"
                    :name="player.name"
                    :singer="player.singer"
                    :current-time="currentTime"
                    :moveEvent="moveEvent"
                    :duration="duration"
                    :volume="volume"
                    :next="next"></action-bar>
      </div>
    </div>
    <div class="bg-image"
         :style="`background-image: url('${albumImage}');`"></div>
  </div>
</template>

<script>
import actionBar from '../action-bar'

export default {
  props: {
    list: {
      type: Array,
      default: () => []
    },
    irc: {
      type: Array,
      default: () => []
    },
    albumImage: {
      type: String,
      default: ''
    },
    player: {
      type: Object,
      default: () => { }
    },
    progressWidth: {
      type: String,
      default: 0
    },
    loop: {
      type: Number,
      default: 0
    },
    isPlaying: {
      type: Boolean,
      default: false
    },
    isActived: {
      type: Function,
      default: () => {
      }
    },
    prev: {
      type: Function,
      default: () => {
      }
    },
    pause: {
      type: Function,
      default: () => {
      }
    },
    play: {
      type: Function,
      default: () => {
      }
    },
    next: {
      type: Function,
      default: () => {
      }
    },
    changeLoop: {
      type: Function,
      default: () => {
      }
    },
    currentTime: {
      type: Number,
      default: 0
    },
    duration: {
      type: Number,
      default: 0
    },
    index: {
      type: Number,
      default: 0
    },
    volume: {
      type: Number,
      default: 0
    },
    moveEvent: {
      type: Function,
      default: () => { }
    }
  },
  data () {
    return {
      visible: false,
      selectIndex: 'default',
      playIndex: 'default',
      offsetHeight: 0,
      selected: [],
      checked: [],
      containerOffset: 0,
      scrollHeight: 0,
      listOffset: 0
    }
  },
  async mounted () {
    this.containerOffset = (document.documentElement.clientHeight - 200) / 2
  },
  components: {
    actionBar
  },
  methods: {
    open () {
      this.visible = true
      return new Promise((resolve) => {
        this._resolve = resolve;
      });
    },
    isPlayIndex ({ song_id }) {
      return this.list[this.index].song_id === song_id
    },
    close () {
      this.visible = false
      this._resolve({ close: true })
    },
    indexNum (index) {
      const l = index + 1
      return l < 10 ? `0${l}` : l
    },
    handleLyric ({ lineNum }) {
      const { offsetTop } = this.$refs.lyricLine[lineNum]
      this.scrollHeight = this.containerOffset - offsetTop
    },
    playSong (song) {
      this.$player.insertSong(song)
    }
  },
  watch: {
    index (newVal, oldVal) {
      this.scrollHeight = this.containerOffset
      const length = this.list.length
      if (length < 8 || newVal < 3) {
        return
      }
      this.$refs.songContainer.scrollTo(0, this.$refs.songLine[newVal - 3].offsetTop)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "./index.scss";
</style>